package com.taniurakengo.sourceviewer;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.taniurakengo.sourceviewer.R;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Window;

/**
 * 
 * @author Taniura
 *
 */
public class SourceViewActivity extends FragmentActivity {

	private ViewPager mViewPager;
	
	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.webview_fragment);

		try {
			//findViewById
			mViewPager = (ViewPager) findViewById(R.id.pager1);

			//get intent
			Intent intent = getIntent();
			String keyword = intent.getStringExtra("CURRENT_PATH");

	    	File file = new File(keyword);
			List<String> items = new ArrayList<String>();

	    	//file？
	    	if(file.isFile()){
				items.add(file.getPath());
	    	}else{
	    		
	    		//folder search
	    		items = this.AddFileList(file, items);
	    	}
	    	
			//set ViewPager   	
	        FragmentManager fm = getSupportFragmentManager();
	        CustomFragmentPagerAdapter adapter = new CustomFragmentPagerAdapter(fm);
	        adapter.setitems(items);
	        adapter.setCurrentpath(keyword);
	        mViewPager.setAdapter(adapter);
			
		} catch (Exception e) {
			System.out.println(e.toString());
		}
        
	}

	/**
	 * 
	 * @param file
	 * @param _items
	 * @return
	 */
	private List<String> AddFileList(File file, List<String> _items){

		try {
			String[] tmp = file.list();
			Arrays.sort(tmp);
			String directory = file.getPath();

			for (int i = 0; i < tmp.length; i++) {
				file = new File(directory + "/" + tmp[i]);
				
				//directory？
				if(file.isDirectory()){
					_items = AddFileList(file, _items);
				}

				//file？
				if(file.isFile()){
					if(IsEnableExtension(file)){
		    			_items.add(directory + "/" + tmp[i]);
					}
				}
				
			}
			
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		return _items;
	}
	
    private boolean IsEnableExtension(File file){
    	
    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

    	if(pref.getBoolean("BRUSH_ALL", false)) return true;
    	
		if (pref.getBoolean("BRUSH_C++", false) && (file.getName().endsWith(".c") || file.getName().endsWith(".cpp") || file.getName().endsWith(".cc"))) return true;
		if (pref.getBoolean("BRUSH_C++", false) && (file.getName().endsWith(".m") || file.getName().endsWith(".h"))) return true;
		if (pref.getBoolean("BRUSH_C#", false) && (file.getName().endsWith(".cs"))) return true;
		if (pref.getBoolean("BRUSH_CSS", false) && (file.getName().endsWith(".css"))) return true;
		if (pref.getBoolean("BRUSH_JAVA", false) && (file.getName().endsWith(".java"))) return true;
		if (pref.getBoolean("BRUSH_PERL", false) && (file.getName().endsWith(".pl"))) return true;
		if (pref.getBoolean("BRUSH_PHP", false) && (file.getName().endsWith(".php")) ) return true;
		if (pref.getBoolean("BRUSH_PYTHON", false) && (file.getName().endsWith(".py")) ) return true;
		if (pref.getBoolean("BRUSH_RUBY", false) && (file.getName().endsWith(".rb")) ) return true;
		if (pref.getBoolean("BRUSH_SCALE", false) && (file.getName().endsWith(".scala")) ) return true;
		if (pref.getBoolean("BRUSH_SQL", false) && (file.getName().endsWith(".sql")) ) return true;
		if (pref.getBoolean("BRUSH_VB", false) && (file.getName().endsWith(".vb") || file.getName().endsWith(".bas")) ) return true;
		if (pref.getBoolean("BRUSH_XML", false) && (file.getName().endsWith(".html") || file.getName().endsWith(".htm")) ) return true;
		if (pref.getBoolean("BRUSH_XML", false) && (file.getName().endsWith(".xml") || file.getName().endsWith(".xhtm")) ) return true;
		if (pref.getBoolean("BRUSH_XML", false) && (file.getName().endsWith(".storyboard")) ) return true;
    	
		return false;
    }
	/**
	 * 
	 */
	@Override
	protected void onStart (){
		super.onStart();
	}
	
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }  
    
    /**
     * 
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
		try {
	    	if(keyCode == KeyEvent.KEYCODE_BACK){

	    		//return FileBrowserActivity
	    		Intent i = new Intent(getApplication(), FileBrowserActivity.class);
				startActivity(i);
				SourceViewActivity.this.finish();
	    		
	    		return false;
	    		
	    	}			
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		
		return super.onKeyDown(keyCode, event);
    	
    }
    

    

}
